﻿using gptDataType.Interface;
using gptDataType.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace gptDataType.Type.combination
{
   public class PlcStruct : PlcComplexBase
    {
        ///// <summary>
        ///// 结构体成员列表
        ///// </summary>
        //private readonly List<IPlcMember> _members = new List<IPlcMember>();

        //// 添加获取成员列表的方法
        //public IReadOnlyList<IPlcMember> GetMembers() => _members.AsReadOnly();

        ///// <summary>
        ///// 结构体构造函数
        ///// </summary>
        ///// <param name="name"></param>
        
        public PlcStruct(string name) : base(name, DataType.Struct, 0) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="customt"></param>
        public PlcStruct(string name,string customt) : base(name, DataType.Custom,0,customt) { }

        ///// <summary>
        ///// 结构体构造函数,创建默认的空结构体
        ///// </summary>
        public PlcStruct() : base("test", DataType.Struct, 0) { }
        //public  int _currentByteOffset { get; protected set; }  // 当前分配到的字节位置
        //public void AddMember(IPlcMember member)
        //{
        //    member.Parent = this;
        //    member.Address = new PlcAddress( _currentByteOffset, 0);
        //    _currentByteOffset += member.SizeInBytes();
            
            
        //    _members.Add(member);
        //    UpdateSize(member);
        //    UpdateSize(member);
        //}

        ///// <summary>
        ///// 结构体中添加一个子元素就要调用此方法更新这个结构体的大小
        ///// </summary>
        //public void UpdateSize(IPlcMember member)
        //{
        //    SizeInBits = getBitSize();
        //}
        //public int getBitSize()
        //{
        //    int res = 0;
        //    for (int i = 0; i < _members.Count; i++)
        //    {
        //        if (_members[i] is PlcStruct pst)
        //        {
        //            res += pst.getBitSize();
        //        }
        //        else
        //        {
        //            res += _members[i].SizeInBits;
        //        }
        //    }
        //    return res;
        //}

        ////调整结构体成员的索引值
        //public void compileAddr(int byteOffset = 0)
        //{

        //   // int _bitOffset = 0;
        //    int _byteOffset = byteOffset;
        //    for (int i = 0; i <_members.Count; i++)
        //    {
        //        //结构体类型的处理
        //        if (_members[i] is PlcStruct pst)
        //        {
        //            pst.Address = new PlcAddress(_byteOffset);
        //            pst.compileAddr( _byteOffset);
        //            //var test = _members[i].Name;
        //            //var test2 = _byteOffset;
        //            _byteOffset += pst.SizeInBytes();
        //        }
        //        //数组类型的处理
        //        else if (_members[i] is PlcArray pa)
        //        {
        //            pa.Address = new PlcAddress(_byteOffset);
        //            pa.compileAddr(_byteOffset);
        //            _byteOffset += pa.SizeInBytes();
        //        }
        //        else
        //        {
        //            var test = _members[i];
        //            _members[i].Address = new PlcAddress(_byteOffset);
        //            _byteOffset += _members[i].SizeInBytes();
        //        }
        //    }
        //}

    }
}
